<template>
  <div class="app-container">
    <div class="app-container-table-border">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="onUploadFile()">附件管理</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['base:bsZd:export']">导出</el-button>
          </el-col>
        </el-row>
      </el-form>

      <el-table border stripe v-loading="loading" :data="dataList" :summary-method="getSummaries" show-summary
        tooltip-effect="dark">
        <el-table-column label="站点名称" align="center" prop="name" min-width="150px" show-overflow-tooltip />
        <el-table-column label="合同金额" align="center" prop="htje" min-width="100px">
        </el-table-column>
        <el-table-column label="开工预付款金额" align="center" prop="kgyfkjr" min-width="100px">
        </el-table-column>
        <el-table-column label="占比（%）" align="center" prop="zb" min-width="100px">
        </el-table-column>
        <el-table-column label="扣除奖励基金" align="center" prop="kcjljj" min-width="100px">
        </el-table-column>
      </el-table>
    </div>
    <file-upload-view ref="fileUploadViewRef" :moduleKey="'yfk'" :disabled="true" />
  </div>
</template>

<script>
import { listDataBsStartAdvance, saveBatch } from "@/api/cm/bsStartAdvance";
import { queryBdSelect } from "@/api/base/bsBd";
import { mapGetters } from "vuex";
import decimal from "@/utils/big-decimal";
import FileUploadView from '@/components/FileUpload/FileUploadView.vue';

export default {
  name: "Yfk",
  dicts: ['report_status'],

  components: {
    FileUploadView
  },
  props: {
    bdId: {
      type: String,
      default: ''
    },
  },
  data () {
    return {
      treeList: [],
      treeInfo: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      isDisabled: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bdId: undefined,
        name: undefined,
        sort: undefined,
        jianscj: undefined,
        jiansrl: undefined,
        danwzj: undefined,
        beiazmc: undefined,
        bdName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "不能为空", trigger: "blur" }],
        bdId: [{ required: true, message: "所属标段id不能为空", trigger: "blur" }],
        name: [{ required: true, message: "站点名称不能为空", trigger: "blur" }],
        jianscj: [{ required: true, message: "建设场景不能为空", trigger: "blur" }],
        jiansrl: [{ required: true, message: "建设容量不能为空", trigger: "blur" }],
        danwzj: [{ required: true, message: "单瓦造价不能为空", trigger: "blur" }],
        beiazmc: [{ required: true, message: "备案证名称不能为空", trigger: "blur" }],
        bdName: [{ required: true, message: "所属标段名称不能为空", trigger: "blur" }]
      },
      multipleSelection: [],
      reportStatus: '00'
    };
  },
  watch: {
    bdId: {
      deep: true,
      immediate: true,
      handler (val) {
        this.queryParams.bdId = val;
        this.handleQuery();
      }
    }
  },
  created () {
  },
  methods: {
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询站点列表 */
    getList () {
      this.loading = true;
      listDataBsStartAdvance(this.queryParams).then((response) => {

        this.dataList = response.data;
        this.reportStatus = '00';
        if (this.dataList.length > 0) {
          this.reportStatus = this.dataList[0].reportFlag;
        }
        // this.total = response.total;
        this.loading = false;
      });
    },
    getSummaries (param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        // if (index === 1) {
        //   sums[index] = this.treeInfo.name || "-";
        //   return;
        // }
        if (index === 1 || index === 2 || index === 4) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return decimal.add(prev, curr);
              } else {
                return decimal.round(prev);
              }
            }, 0);
            sums[index] += " 元";
          } else {
            sums[index] = "";
          }
        }
      });

      return sums;
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        bdId: this.treeInfo.id,
        name: undefined,
        remark: undefined,
        sort: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        jianscj: undefined,
        jiansrl: undefined,
        danwzj: undefined,
        beiazmc: undefined,
        bdName: this.treeInfo.name
      };
      this.resetForm("form");
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "cm/bsSettle/export",
        {
          ...this.queryParams
        },
        `bsSettle_${new Date().getTime()}.xlsx`
      );
    },
    // 附件上传
    onUploadFile () {
      const info = {
        businessId: this.sysProjectId,
        subBusinessId: this.queryParams.bdId,
      };
      this.$refs.fileUploadViewRef.init(info);
    },
  }
};
</script>
<style lang="scss" scoped>
.boxCenter {
  margin-top: 20px;
  text-align: center;
}
</style>
